package com.lej.login.guardSpirit

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.core.animation.addListener
import androidx.core.view.isVisible
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.lej.core.base.BaseActivity
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.toast
import com.lej.core.utils.extention.versionName
import com.lej.dataservice.club.bean.login.SpiritMsg
import com.lej.dataservice.club.http.bean.onError
import com.lej.dataservice.club.http.bean.onSuccess
import com.lej.login.BR
import com.lej.login.R
import com.lej.login.databinding.ActivityGuardianSpiritBinding
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.util.QMUIStatusBarHelper

/**
 *  author: cjq
 *  date  : 2022/10/6 0006
 *  desc  :
 */

@Route(path = CRouter.guardianSpirit)
class GuardianSpiritActivity : BaseActivity<ActivityGuardianSpiritBinding, GuardianSpiritVM>(){

    private var msgData: SpiritMsg? = null
    var adapter: GuardianSpiritAdapter? = null
    var outIndex = 0
    private var innerIndex = 0
    var currentTime = 0L
    var mInterpolator = AccelerateDecelerateInterpolator()
    private var downHeight = 0f  //蛋下落的地
    private var moveWidth = 0f //蛋水平移动的地
    private var upHeight = 0f //蛋上升的地

    override fun getLayout(): Int = R.layout.activity_guardian_spirit

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        super.initData()
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        downHeight = QMUIDisplayHelper.getScreenHeight(this@GuardianSpiritActivity) * 0.7.toFloat()
        upHeight = QMUIDisplayHelper.getScreenHeight(this@GuardianSpiritActivity) * 0.2.toFloat()
        moveWidth = QMUIDisplayHelper.getScreenWidth(this@GuardianSpiritActivity) * 0.2.toFloat()
        viewModel.getSpiritMsg(versionName).onSuccess(this) {
            msgData = Gson().fromJson(it, SpiritMsg::class.java)
            startEggAnim()
        }.onError(this) {
        }
    }

    private fun startEggAnim(){
        // 文字透明度
        val textAlphaAnim = ObjectAnimator.ofFloat(binding.clName, "alpha", 0f, 1f, 0f).apply {
            duration = 3000
            interpolator = mInterpolator
            addListener(onStart = {
                binding.clName.isVisible = true
            }, onEnd = {
                viewModel.isWelcome.value = false
                binding.ivEgg.isVisible = true
                binding.ivChatBg.isVisible = true
            })
        }
        //下落
        val downAnim = ObjectAnimator.ofFloat(
            binding.ivEgg,
            "translationY",
            0f,
            downHeight
        ).apply {
            duration = 1000
            interpolator = mInterpolator
        }
        // 水平，竖直，旋转
        val transYAnim = ObjectAnimator.ofFloat(
            binding.ivEgg,
            "translationY",
            downHeight,
            upHeight
        ).apply {
            interpolator = mInterpolator
            duration = 1000
        }
        val transXAnim = ObjectAnimator.ofFloat(
            binding.ivEgg,
            "translationX",
            0f,
            moveWidth
        ).apply {
            interpolator = mInterpolator
            duration = 1000
        }
        val roaAnim = ObjectAnimator.ofFloat(binding.ivEgg, "rotation", 0f, 20f)
            .apply { duration = 1000 }

        // 抖动
        val rippleAnim = ObjectAnimator.ofFloat(
            binding.ivEgg,
            "rotation",
            20f,
            0f,
            -20f,
            0f,
            20f,
            0f
        ).apply {
            duration = 1000
            addListener(onStart = {
                binding.ivRipple.isVisible = true
            }, onEnd = {
                showChatMsg()
            })
        }
        //呼吸
        val scaleX =
            ObjectAnimator.ofFloat(binding.ivRipple, "scaleX", 1f, 1.3f)
                .apply {
                    duration = 1000
                    repeatCount = ValueAnimator.INFINITE
                    repeatMode = ValueAnimator.REVERSE
                }
        val scaleY =
            ObjectAnimator.ofFloat(binding.ivRipple, "scaleY", 1f, 1.3f)
                .apply {
                    duration = 1000
                    repeatCount = ValueAnimator.INFINITE
                    repeatMode = ValueAnimator.REVERSE
                }

        AnimatorSet().apply {
            play(textAlphaAnim).before(downAnim)
            play(downAnim).before(transXAnim)
            play(transXAnim).with(transYAnim).with(roaAnim)
            play(rippleAnim).with(scaleX).with(scaleY).after(roaAnim)
            start()
        }
    }

    private fun showChatMsg() {
        binding.ivBottom.isVisible = true
        binding.rv.isVisible = true
        binding.tvUnKnown.isVisible = true
        binding.tvChat.isVisible = true

        adapter = GuardianSpiritAdapter(R.layout.item_guardian_spirit)
        binding.rv.adapter = adapter
        setChatText()
        binding.ivChatBg.onClick {
            if (System.currentTimeMillis() - currentTime > 500) {
                setChatText()
                currentTime = System.currentTimeMillis()
            }
        }
    }

    private fun setChatText() {
        if (outIndex >= msgData?.chatList?.size ?: 0) {
            //todo 对话完成
            "结束".toast()
            return
        }
        if(outIndex == (msgData?.chatList?.size ?: 0) - 1){
            binding.ivEgg.setImageResource(R.drawable.ic_guardian_spirit_robot)
        }
        val chatMsg = msgData?.chatList?.get(outIndex)
        adapter?.setList(chatMsg?.userMsg)
        binding.tvChat.text = chatMsg?.spiritMsg?.get(innerIndex) ?: ""
        innerIndex += 1

        if (innerIndex >= chatMsg?.spiritMsg?.size ?: 0) {
            outIndex += 1
            innerIndex = 0
        }
    }
}